/*******************************************************************************
 * * Copyright 2013 Impetus Infotech.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 ******************************************************************************/
package com.impetus.client.es;

import java.util.Map;
import java.util.Properties;

import com.impetus.kundera.configure.AbstractPropertyReader;
import com.impetus.kundera.configure.ClientProperties;
import com.impetus.kundera.configure.ClientProperties.DataStore;
import com.impetus.kundera.configure.ClientProperties.DataStore.Connection;
import com.impetus.kundera.configure.PropertyReader;
import com.impetus.kundera.metadata.model.PersistenceUnitMetadata;

/**
 * The Class ESClientPropertyReader.
 * 
 * @author vivek.mishra
 */
public class ESClientPropertyReader extends AbstractPropertyReader implements PropertyReader
{

    /** The esmd. */
    private ESSchemaMetadata esmd;

    /**
     * Instantiates a new ES client property reader.
     * 
     * @param externalProperties
     *            the external properties
     * @param puMetadata
     *            the pu metadata
     */
    public ESClientPropertyReader(Map externalProperties, final PersistenceUnitMetadata puMetadata)
    {
        super(externalProperties, puMetadata);
        esmd = new ESSchemaMetadata();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.impetus.kundera.configure.AbstractPropertyReader#onXml(com.impetus
     * .kundera.configure.ClientProperties)
     */
    @Override
    protected void onXml(ClientProperties cp)
    {
        if (cp != null)
        {
            esmd.setClientProperties(cp);
        }
    }

    /**
     * Gets the connection properties.
     * 
     * @return the connection properties
     */
    public Properties getConnectionProperties()
    {
        return esmd != null ? esmd.getConnectionProperties() : null;
    }

    /**
     * The Class ESSchemaMetadata.
     */
    private class ESSchemaMetadata extends AbstractSchemaMetadata
    {

        public Properties getConnectionProperties()
        {
            DataStore ds = getDataStore("elasticsearch");

            if (ds != null)
            {
                Connection connection = ds.getConnection();
                if (connection != null)
                {
                    Properties properties = connection.getProperties();

                    return properties;
                }
            }
            return null;
        }
    }
}
